<template>
    <a-modal :keyboard="false" :maskClosable="false" title="抽检" :open="visible" :width="550" :confirmLoading="loading" centered @ok="handleSubmit" @cancel="handleCancel">
        <a-form layout="vertical">
            <a-row :gutter="16" type="flex" justify="start">
                <a-col :xs="24" :sm="24" :md="24">
                    <a-form-item label="是否合格" v-bind="validateInfos.checkResult">
                        <a-radio-group v-model:value="modelRef.checkResult" @change="checkResultChange" :disabled="modelRef.spotCheck == 1">
                            <a-radio :value="1">合格</a-radio>
                            <a-radio :value="0">不合格</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="24">
                    <a-form-item label="说明" v-bind="validateInfos.checkContent">
                        <a-textarea v-model:value="modelRef.checkContent" show-count :maxlength="500" :disabled="modelRef.spotCheck == 1"></a-textarea>
                    </a-form-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="24">
                    <a-form-item label="上传附件">
                        <a-image-preview-group v-if="modelRef.spotCheck == 1">
                            <a-space>
                                <template v-for="(item, index) in modelRef.checkRecord" :key="index">
                                    <audio controls :src="item.viewAddress" v-if="item.oldName.includes('.mp3') || item.oldName.includes('.m4a')"></audio>
                                    <a-image v-else :width="80" :height="80" :src="item.viewAddress" />
                                </template>
                            </a-space>
                        </a-image-preview-group>
                        <custom-upload v-else v-model:fileList="modelRef.checkRecord"></custom-upload>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-modal>
</template>

<script lang="ts">
    import type { PropType } from 'vue';
    import { defineComponent, reactive, ref, watchEffect, watch } from 'vue';
    import { Form, message } from 'ant-design-vue';
    import { cloneDeep, isEmpty } from 'lodash-es';
    import { closeOrder, checkWarranty, spotCheckOrder } from '@/api/order/mine-list';

    export default defineComponent({
        props: {
            visible: {
                type: Boolean,
                required: true,
            },
            model: {
                type: Object as PropType<any>,
                default: () => null,
            },
        },
        emits: ['cancel', 'success'],
        setup(props, { emit }) {
            const loading = ref(false);

            const defaultForm = {
                checkResult: 1,
                checkContent: '',
                checkRecord: [],
            };
            const modelRef = reactive<any>(cloneDeep(defaultForm));
            const rulesRef = reactive({
                checkResult: [{ required: true, message: '请选择' }],
                checkContent: [{ required: false, message: '请选择' }],
                // checkRecord: [{ required: true, message: '请上传', type: 'array' }],
            });
            const { validateInfos, resetFields, validate } = Form.useForm(modelRef, rulesRef);

            // 记录初始值
            const initValues = reactive({});
            watch(
                () => props.visible,
                () => {
                    Object.assign(initValues, cloneDeep(props.model));
                },
            );
            watchEffect(() => {
                if (props.visible) {
                    if (props.model) {
                        Object.assign(modelRef, {
                            orderId: props.model.id,
                            spotCheck: props.model.spotCheck,
                            checkResult: props.model.checkResult,
                            checkRecord: props.model.checkRecord,
                            checkContent: props.model.checkContent,
                        });
                    }
                }
            });

            const handleCancel = () => {
                resetFields();
                Object.keys(modelRef).forEach(key => {
                    delete modelRef[key];
                });
                Object.assign(modelRef, cloneDeep(defaultForm));
                modelRef.status = 1;
                emit('cancel');
            };
            const checkResultChange = (e: any) => {
                rulesRef.checkContent[0].required = e.target.value === 0;
            };

            const handleSubmit = async () => {
                validate().then(() => {
                    loading.value = true;
                    spotCheckOrder({
                        ...modelRef,
                    })
                        .then(res => {
                            if (res.data) {
                                message.success('操作成功');
                                emit('success');
                                handleCancel();
                            }
                        })
                        .finally(() => {
                            loading.value = false;
                        });
                });
            };
            return {
                checkResultChange,
                loading,

                modelRef,
                validateInfos,
                resetFields,

                handleSubmit,
                handleCancel,

                initValues,
                isEmpty,
            };
        },
        components: {},
    });
</script>
